//
//  File.swift
//  
//
//  Created by CSS on 8/17/23.
//

import Foundation

postfix operator <->
struct LanguageModel {
    
    var key: String?
    var value: String?
    
    static func ==(left: LanguageModel, right: LanguageModel?) -> Bool {
        left.key == right?.key
    }
    
    static func !=(left: LanguageModel, right: LanguageModel?) -> Bool {
        left.key != right?.key
    }
    
}

struct LanguageSectionModel {
    var key: String?
    var languageModels: [LanguageModel] = []
    
    var keyInHeaderView: String? {
        guard let key else {
            return "未知分区"
        }
        return "\(key)文件共\(languageModels.count)条记录"
    }
    
    func filterModel(withSearchText searchText: String) -> LanguageSectionModel? {
        let languageModels = languageModels.filter{ $0.key?.localizedCaseInsensitiveContains(searchText) ?? false }
        if languageModels.count == 0 {
            return nil
        }
        return LanguageSectionModel(key: key, languageModels: languageModels)
    }
}

extension Array where Element == LanguageSectionModel {
    
    func languageModel(atIndexPath indexPath: IndexPath) -> LanguageModel? {
        if count <= indexPath.section {
            return nil
        }
        let sectionModel = self[indexPath.section]
        if sectionModel.languageModels.count <= indexPath.row {
            return nil
        }
        return sectionModel.languageModels[indexPath.row]
    }
    
    var sectionNums: Int {
        count
    }
    
    func rowNums(inSection section: Int) -> Int {
        if count <= section {
            return 0
        }
        return self[section].languageModels.count
    }
    
    var totalLanguageModelNums: Int {
        reduce(0, { $0 + $1.languageModels.count })
    }
    
}
